package com.qeado.content.web;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.qeado.admin.common.BaseConstData;
import com.qeado.common.helper.AttributeHelper;
import com.qeado.content.model.CompanyInfo;
import com.qeado.content.service.CompanyInfoService;

@Controller
public class CompanyInfoController {
	@Autowired
	private CompanyInfoService companyInfoService;
	
	@RequestMapping("/companyInfoInit.ac")
	public String init(HttpServletRequest request) {
		AttributeHelper.removeSessionAttribute(request, "T_");
		List<CompanyInfo> companyCategoryList = companyInfoService.getCompanyCategoryList();
		List<CompanyInfo> companyIndustryList = companyInfoService.getCompanyIndustryList();
		List<CompanyInfo> companyIndustryCategoryList = companyInfoService.getCompanyIndustryCategoryList();
		List<CompanyInfo> companyManagementModeList = companyInfoService.getCompanyManagementModeList();
		request.getSession().setAttribute("T_companyCategoryList", companyCategoryList);
		request.getSession().setAttribute("T_companyIndustryList", companyIndustryList);
		request.getSession().setAttribute("T_companyIndustryCategoryList", companyIndustryCategoryList);
		request.getSession().setAttribute("T_companyManagementModeList", companyManagementModeList);
		return "content/company_info_search";
	}
	
	@RequestMapping("/companyInfoSearch.ac")
	public ModelAndView companyInfoSearch(CompanyInfo companyInfo) {
		List<CompanyInfo> companyList = companyInfoService.searchCompanyInfo(companyInfo);
		return new ModelAndView("content/company_info_search", "companyList", companyList);
	}
	
	@RequestMapping("/companyInfoNew.ac")
	public String companyInfoNew() {
		return "content/company_info_manage";
	}
	
	@RequestMapping("/backToCompanySearch.ac")
	public String backToCompanySearch() {
		return "content/company_info_search";
	}
	
	@RequestMapping("/companyInfoSelect.ac")
	public ModelAndView companyInfoSelect(String id) {
		CompanyInfo companyInfo = companyInfoService.searchCompanyInfoById(id);
		return new ModelAndView("content/company_info_manage", "companyInfo", companyInfo);
	}
	
	@RequestMapping("/companyInfoSave.ac")
	public ModelAndView companyInfoSave(HttpServletRequest request, CompanyInfo companyInfo) throws IOException {
		companyInfo.setCreator(request.getSession().getAttribute(BaseConstData.USER_ID).toString());
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
		MultipartFile file = multipartRequest.getFile("localFile");
		if(!file.isEmpty()) {
			companyInfo.setFileName(file.getOriginalFilename());
		}
		companyInfo.setCompanyLogo(file.getBytes());
		String message = companyInfoService.saveCompanyInfo(companyInfo);
		return new ModelAndView("content/company_info_manage", "message", message);
	}
	
	@RequestMapping("/companyInfoDelete.ac")
	public ModelAndView companyInfoDelete(String id) {
		CompanyInfo companyInfo = new CompanyInfo();
		companyInfo.setId(id);
		companyInfoService.deleteCompanyInfo(companyInfo);
		List<CompanyInfo> companyList = companyInfoService.searchCompanyInfo(null);
		return new ModelAndView("content/company_info_search", "companyList", companyList);
	}
	
	@RequestMapping("/getCompanyLogo.ac")
	public void getCompanyLogo(HttpServletResponse response,String id) throws UnsupportedEncodingException {
		CompanyInfo companyInfo = companyInfoService.getCompanyLogoById(id);
		String fileName = companyInfo.getFileName();
		fileName = URLEncoder.encode(fileName,"UTF-8");
	    response.addHeader("Content-Disposition","attachment;filename=" + fileName);
	    OutputStream output = null;
	    ByteArrayInputStream fis = null;
	    try {
	        output = response.getOutputStream();
	        fis = new ByteArrayInputStream(companyInfo.getCompanyLogo());
	        byte[] b = new byte[1024];
	        int i = 0;
	        while((i = fis.read(b)) > 0) {
	            output.write(b, 0, i);
	        }
	        output.flush();
	    } catch(Exception e) {
	        e.printStackTrace();
	    } finally {
	        if(fis != null) { 
	        	try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
	        }
	        if(output != null) {
	        	try {
					output.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
	        }
	    }
	}

}
